<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Insert title here</title>
		<script type="text/javascript" language="javascript">
			var xmlhttp = null;
			var flag = false;
			window.onload = function(){
				document.getElementById("form").onsubmit = function(){
					var fchecked = checkForm();
					return fchecked && flag;
				};
				document.getElementById("idname").onblur = function(){
					var name = document.getElementById("idname").value;
					checkName(name);
				};
			};
			function checkName(name){
				if(window.XMLHttpRequest){
					xmlhttp = new XMLHttpRequest();
				}else{
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}
				xmlhttp.open("POST","/test/userOperateServlet?useroperate=isexist&name="+name);
				xmlhttp.onreadystatechange = function(){
					if(xmlhttp.readyState==4 && xmlhttp.status==200 ){
						var text = xmlhttp.responseText;
						var iderrormsg = document.getElementById("iderrormsg");
						if(text=="yes"){
							iderrormsg.textContent="Available";
							flag = true;
						}else{
							iderrormsg.textContent="Unavailable";
							flag = false;
						}
					}
				};
				xmlhttp.send();
			}
			
			function checkForm(){
				var iderrormsg = document.getElementById("iderrormsg");
				var form = document.getElementById("form");
				var name = form.name.value;
				var password = form.password.value;
				var regex = /^\S+$/;
				if(!regex.test(name)){
					iderrormsg.textContent="name?";
					return false;
				}
				if(!regex.test(password)){
					iderrormsg.textContent="password?";
					return false;
				}
				return true;
			}
		</script>
	</head>
	<body>
		<jsp:include page="/jspdemo/header.jsp" />
		<form id="form" action="/test/userOperateServlet?useroperate=regist" method="post">
			<table border="1" cellspacing="0" bordercolor="red">
				<tr>
					<td>用户名:</td>
					<td><input id="idname" type="text" name="name">
					</td>
				</tr>
				<tr>
					<td>密码:</td>
					<td><input type="password" name="password">
					</td>
				</tr>
				<tr>
					<td><input type="reset" value="重置">
					</td>
					<td><input type="submit" value="注册">
					</td>
				</tr>
			</table>
		</form>
		<label id="iderrormsg" style="color:red">${registmsg }</label>
	</body>
</html>